/* Copyright (c) 2021-2022 Qualcomm Technologies International, Ltd. */
/* %%version */

#include "gatt_cas_server_common.h"
#include "gatt_cas_server_msg_handler.h"
#include "gatt_cas_server_access.h"
#include "gatt_cas_server_debug.h"

/******************************************************************************/
void casServerMsgHandler(void* task, MsgId id, Msg msg)
{
    GCAS *cas_server = (GCAS *)task;

    switch (id)
    {
        case CSR_BT_GATT_REGISTER_CFM:
        {
            CsrBtGattRegisterCfm* cfm;
            GATT_CAS_SERVER_DEBUG(" \n GCAS Server: CSR_BT_GATT_REGISTER_CFM recievced \n");

            cfm = (CsrBtGattRegisterCfm*)msg;
            if (cfm->resultCode == CSR_BT_GATT_RESULT_SUCCESS &&
                    cfm->resultSupplier == CSR_BT_SUPPLIER_GATT)
            {
                cas_server->gattId = cfm->gattId;

                CsrBtGattFlatDbRegisterHandleRangeReqSend(cas_server->gattId,
                                                          cas_server->startHandle,
                                                          cas_server->endHandle);
            }
            break;
        }
        case CSR_BT_GATT_DB_ACCESS_WRITE_IND:
        {
            /* Nothing to do */
        }
        break;

        case CSR_BT_GATT_DB_ACCESS_READ_IND:
        {
            /* Nothing to do */
        }
        break;

        default:
        {
            GATT_CAS_SERVER_WARNING(
                        "GCAS: GATT Manager message 0x%04x not handled\n",
                        id
                        );
            break;
        }
    } /* switch */

    CsrBtGattFreeUpstreamMessageContents(CSR_BT_GATT_PRIM, msg);
}

/* Synergy task scheduler message handler */

void cas_server_msg_handler(void** gash)
{
    void* msg = NULL;
    CsrBtGattPrim id;
    uint16 eventClass = 0;
    ServiceHandle srvc_hndl = 0;

    srvc_hndl = *((ServiceHandle*)*gash);

    if (CsrSchedMessageGet(&eventClass, &msg))
    {
        switch (eventClass)
        {
        case CSR_BT_GATT_PRIM:
        {
            GCAS *vcs = ServiceHandleGetInstanceData(srvc_hndl);

            if (vcs)
            {
                id = *(CsrBtGattPrim*)msg;
                casServerMsgHandler(vcs, id, msg);
            }
            break;
        }
        case CAS_SERVER_PRIM:
        default:
            break;
        }

        SynergyMessageFree(eventClass, msg);
    }
}

